﻿/*  
    Copyright (C) <2007-2013>  <Kay Diefenthal>

    ArgusTv.UI.MediaCenter is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ArgusTv.UI.MediaCenter is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ArgusTv.UI.MediaCenter.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace ArgusTV.UI.MediaCenter
{
    public class Utils
    {
        private static readonly Ping PingSender = new Ping();

        public static String RebuildMediaStreamUrl(String protocol, String hostname, String port, String media)
        {
            return protocol + "://" + hostname + ":" + port + "/" + media;
        }

        public static String GetIpAddress(String value)
        {
            string str = null;
            var hostentry = Dns.GetHostEntry(value);
            foreach (var ip in hostentry.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork))
            {
                str = ip.ToString();
            }
            return str;
        }

        [DllImport("iphlpapi.dll")]
        public static extern int SendARP(int destIp, int srcIp, [Out] byte[] pMacAddr, ref int phyAddrLen);

        public static string GetMacAddress(string ip)
        {
            var addr = IPAddress.Parse(ip);
            var mac = new byte[6];
            var length = mac.Length;
            SendARP((int) addr.Address, 0, mac, ref length);
            var macAddress = BitConverter.ToString(mac, 0, length);
            return macAddress;
        }

        public static String GetLocalHostName()
        {
            return Dns.GetHostName();
        }

        public static Boolean SinglePingTarget(string address, int timeout)
        {
            PingReply reply;

            try
            {
                reply = PingSender.Send(address, timeout);
            }
            catch
            {
                return false;
            }

            if (reply != null && reply.Status == IPStatus.Success)
                return true;
            return false;
        }

        public static Boolean PingTarget(string address, int numberOfPings, int timeout)
        {
            int successCount = 0, failCount = 0;

            for (int i = 0; i < numberOfPings; i++)
            {
                if (SinglePingTarget(address, timeout))
                    successCount++;
                else
                    failCount++;
            }
            return successCount > failCount;
        }
    }
}